using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using BitMango.Diagnostics;

using JsonFx.Json;
using UnityEngine;

namespace BitMango.Promotion
{
    public class Configuration
    {
        private void UpdateZoneList()
        {
            this.UpdateZoneListByResource();
            try
            {
                TaskManager.StartCoroutine(this.UpdateZoneListByRemoteConfig());
            }
            catch (Exception ex)
            {
                Debugger.LogError("Configuration UpdateZoneListByRemoteConfig " + ex.Message);
            }
        }

        private static void FetchComplete(Task fetchTask)
        {
            if (fetchTask.IsCanceled)
            {
                Debugger.Log("Fetch canceled at Configuration.FetchDataAsync");
            }
            else if (fetchTask.IsFaulted)
            {
                Debugger.Log("Fetch encountered an error at Configuration.FetchDataAsync");
            }
            else if (fetchTask.IsCompleted)
            {
                Debugger.Log("Fetch completed successfully at Configuration.FetchDataAsync");
            }
        }

        private IEnumerator UpdateZoneListByRemoteConfig()
        {
            while (!Platform.FinishedCheckFirebase)
            {
                yield return null;
            }
            yield break;
        }

        private void UpdateZoneListByPrefs()
        {
            try
            {
            }
            catch (JsonDeserializationException ex)
            {
                Debugger.LogError("Promotion.UpdateZoneListByPrefs JsonDeserializationException : " + ex.Message);
            }
        }

        private void UpdateZoneListByResource()
        {
            TextAsset textAsset = Resources.Load("InterstitialZone") as TextAsset;
            if (textAsset == null || string.IsNullOrEmpty(textAsset.text))
            {
                Debugger.Assert(false, "Configuration.LoadZoneList zonelist is null or empty!!");
                return;
            }
            try
            {
            }
            catch (JsonDeserializationException ex)
            {
                int num;
                int num2;
                ex.GetLineAndColumn(textAsset.text, out num, out num2);
                Debugger.LogError(string.Concat(new object[]
                {
                    "AdManager.LoadFromJson JsonDeserializationException : ",
                    ex.Message,
                    " on Line:",
                    num,
                    "  Col:",
                    num2
                }));
            }
        }

        private static string ResourceJson
        {
            get
            {
                string text = "adconfig";
                text += ".android";
                TextAsset textAsset = Resources.Load(text) as TextAsset;
                if (textAsset == null)
                {
                    Debugger.LogError("adconfig not found : " + text);
                    return null;
                }
                return textAsset.text;
            }
        }

        public static Configuration GetConfig()
        {
            Configuration configuration = Configuration.LoadFromJson(Configuration.ResourceJson);
            if (configuration == null)
            {
                return null;
            }
            configuration.UpdateZoneList();
            return configuration;
        }

        public static Configuration LoadFromJson(string json)
        {
            Configuration configuration = new Configuration();
            try
            {
                Dictionary<string, object> dictionary = JsonReader.Deserialize<Dictionary<string, object>>(json);
                Dictionary<string, object>[] array = dictionary["order"] as Dictionary<string, object>[];
                foreach (Dictionary<string, object> dictionary2 in array)
                {
                    Order item = new Order((string)dictionary2["adNetwork"], (string)dictionary2["adType"], (string[])dictionary2["placementID"], (string)dictionary2["primaryKey"]);
                    configuration.order.Add(item);
                }
            }
            catch (JsonDeserializationException ex)
            {
                int num;
                int num2;
                ex.GetLineAndColumn(json, out num, out num2);
                Debugger.LogError(string.Concat(new object[]
                {
                    "AdManager.LoadFromJson JsonDeserializationException : ",
                    ex.Message,
                    " on Line:",
                    num,
                    "  Col:",
                    num2
                }));
            }
            return configuration;
        }

        public bool IsMatchCondition(string zoneName, bool isEnter)
        {
            return false;
        }


        public List<Order> GetOrdersByFilter(AdManager.AdType typeFilter, string networkFilter = null)
        {
            List<Order> list = new List<Order>();
            foreach (Order order in this.order)
            {
                if (order.adType == typeFilter && (networkFilter == null || order.adNetwork == networkFilter))
                {
                    list.Add(order);
                }
            }
            if (list.Count == 0)
            {
                return null;
            }
            return list;
        }

        public string Validate()
        {
            if (this.order == null)
            {
                return "Error: order is null";
            }
            bool flag = true;
            bool flag2 = true;
            bool flag3 = true;
            string text = string.Empty;
            text = "admob";
            foreach (Order order in this.order)
            {
                if (order.adType == AdManager.AdType.BANNER && order.adNetwork != text)
                {
                    flag = false;
                }
                if (order.adType == AdManager.AdType.INTERSTITIAL && order.adNetwork != text)
                {
                    flag2 = false;
                }
                if (order.adType == AdManager.AdType.VIDEO && order.adNetwork != text)
                {
                    flag3 = false;
                }
            }
            if (!flag || !flag2 || !flag3)
            {
                return "Error: " + text + " is not exist!";
            }
            return "success";
        }

        private const int ADMANAGER_VERSION = 3;

        public List<Order> order = new List<Order>();


        [CompilerGenerated]
        private static Action<Task> _003C_003Ef__mg_0024cache0;
    }
}
